package cn.com.pxto.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.json.JSONArray;
import org.json.JSONObject;

import cn.com.pxto.commons.httputil.ParamUtil;

import cn.com.pxto.model.Dic;
import cn.com.pxto.service.DicManager;

/**
 * @author Gauss Cheung
 *
 * Jul 7, 2006
 */
public class DicAction extends DispatchAction {
	private DicManager dicManager;
	
	/*
	 * set by spring ioc
	 */
	public void setDicManager(DicManager dicManager) {
		this.dicManager = dicManager;
	}

	/**
	 * ��ȡɸѡ����ֵ��б?����֯��json��ʽ������
	 */
	public ActionForward get(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String type = ParamUtil.getParams(request,"type",null);
		if (type!=null){
			JSONArray result = new JSONArray();
			JSONObject defaultJ = new JSONObject();
			defaultJ.put("value","-1");
			defaultJ.put("label","");
			result.put(defaultJ);
			Iterator dicIter = dicManager.getDicsByType(type).iterator();
			//��������ĿId
			int skip = ParamUtil.getIntParams(request,"skip",-1);
			//�Ƿ�ʹ�������Ϊselect�б��value
			String strvalue=ParamUtil.getParams(request,"nameval","false").toLowerCase();
			while (dicIter.hasNext()){
				
				Dic dic = (Dic)dicIter.next();
				//���IdΪskip����Ŀ
				if(skip>=0 && dic.getId().intValue()==skip)
					continue;
				JSONObject jo = new JSONObject();
				if(strvalue.equals("true")){
					jo.put("value",dic.getName());
				}else{
					jo.put("value",dic.getId().toString());
				}
				jo.put("label",dic.getName());
				result.put(jo);
			}
			response.setCharacterEncoding("GBK");
			response.setContentType("text/json");
			PrintWriter out = response.getWriter();
			System.out.println(result);
			out.print(result.toString());
		}
		return null;
	}
	/**
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward tokenPreAddDic(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		return mapping.findForward("editChannelType");
	}

	
	public ActionForward tokenPreAddDicForFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		return mapping.findForward("editFileType");
	}
	public ActionForward tokenPreAddDicForFileImage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		return mapping.findForward("listFileType");
	}
	
	public ActionForward tokenPreupdateDic(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		int pk = ParamUtil.getIntParams(request,"pk");
		Dic dic = null;
		if(pk>0){
			dic = dicManager.getDic(new Integer(pk));
		}
		request.setAttribute("dic",dic);
		return mapping.findForward("editChannelType");
	}
	public ActionForward tokenPreupdateDicForFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		int pk = ParamUtil.getIntParams(request,"pk");
		Dic dic = null;
		if(pk>0){
			dic = dicManager.getDic(new Integer(pk));
		}
		request.setAttribute("dic",dic);
		return mapping.findForward("editFileType");
	}
	public ActionForward tokenDoupdateDic(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		DynaActionForm dicForm = (DynaActionForm)form;
		Dic dic = (Dic)dicForm.get("dic");
		dicManager.modifyDic(dic);
		request.setAttribute("succ","SUCC");
		return this.listDicChannelType(mapping,form,request,response);
	}
	public ActionForward tokenDoupdateDicForFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		DynaActionForm dicForm = (DynaActionForm)form;
		Dic dic = (Dic)dicForm.get("dic");
		dicManager.modifyDic(dic);
		request.setAttribute("succ","SUCC");
		return this.listDicFileType(mapping,form,request,response);
	}
	public ActionForward listDicChannelType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//int pageNo = ParamUtil.getIntParams(request, "pageNo");
		//PageInfo pageInfo =(PageInfo)(pageNo<=0?PageInfoFactory.getPageInfo(20,1):PageInfoFactory.getPageInfo(20,pageNo));//��ҳ��Ϣ
		List channelTypeList = dicManager.getDicsByType("live_channel"); 
		request.setAttribute("channelType",channelTypeList);
		return mapping.findForward("listChannelType");
	}
	public ActionForward listDicFileType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//int pageNo = ParamUtil.getIntParams(request, "pageNo");
		//PageInfo pageInfo =(PageInfo)(pageNo<=0?PageInfoFactory.getPageInfo(20,1):PageInfoFactory.getPageInfo(20,pageNo));//��ҳ��Ϣ
		List fileTypeList = dicManager.getDicsByType("movie"); 
		request.setAttribute("fileType",fileTypeList);
		return mapping.findForward("listFileType");
	}
	public ActionForward deleteChannelType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		int pk = ParamUtil.getIntParams(request,"pk");
		if(pk>0){
			dicManager.deleteDic(new Integer(pk));
		}
		return this.listDicChannelType(mapping,form,request,response);
	}
	public ActionForward deleteFileType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		int pk = ParamUtil.getIntParams(request,"pk");
		if(pk>0){
			dicManager.deleteDic(new Integer(pk));
		}
		return this.listDicFileType(mapping,form,request,response);
	}
}
